package de.unibi.mmi.homeauto.team1.drafts;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import de.unibi.mmi.homeauto.team1.R;
import de.unibi.mmi.homeauto.team1.mainview.MainViewActivity;

public abstract class DraftFragment extends Fragment {
	
	protected int backgroundID = 0;

	private boolean initiated = false;

	protected MainViewActivity mva;

	public abstract String getTitle();

	@Override
	public final void onAttach(Activity activity) {
		super.onAttach(activity);
		mva = (MainViewActivity) activity;
	}

	@Override
	public final View onCreateView(LayoutInflater inflater,
			ViewGroup container, Bundle savedInstanceState) {

		View view;
		if (this instanceof ImageFragment) {
			view = inflater.inflate(R.layout.image, container, false);

		} else {
			view = inflater.inflate(R.layout.draftfragment, container, false);

			((LinearLayout) view
					.findViewById(R.id.draftfragment_mainlinearLayout))
					.addView(inflater.inflate(getLayoutID(), container, false));
		}

		if(backgroundID != 0){
			view.setBackgroundResource(backgroundID);
		}
		loadComponents(view);
		serverValuesUpdated();
		initiated = true;
		return view;
	}

	protected abstract int getLayoutID();

	protected abstract void loadComponents(View view);

	public final void updateFromServer() {
		if (this.initiated) {
			serverValuesUpdated();
		}
	}

	protected abstract void serverValuesUpdated();

}
